#include<iostream>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<string.h>
#include<errno.h>
using namespace std;

int main()
{
  pid_t id = fork();
 
  if(id < 0)
  {
     printf("errno : %d -> %s\n", errno, strerror(errno));//判断子进程是否创建成功
  }
  else if(id ==  0)
  {
     int cnt = 5;
     while(cnt--)//循环打印子进程便于观察退出
     {	
	     printf("child id = %d\n", getpid());
	     sleep(1);	
     }
     //int* p = nullptr;
     //*p = 1;//制作错误让OS释放信号终止该进程
     exit(123);//制造退出状态
  }
  else
  {
	while(true)
	{
	int status = 0;
	//pid_t rid = wait(nullptr);
	pid_t rid = waitpid(-1, &status, WNOHANG);

	if(rid == 0)
	{
		printf("parent yun\n");
		sleep(1);
	}
	else if(rid > 0)
	{
		if(WIFEXITED(status))
		{
			printf("status code: %d\n", WEXITSTATUS(status));
			sleep(1);
		}
		else
		{
			printf("children process quit error\n");
		}
    	}
	else
	{
		sleep(1);
		printf("child fail\n");
	}
 	}
}
  return 0;
}




//#include<stdio.h>
//#include<unistd.h>
//#include<stdlib.h>
//
//int main()
//{
//  printf("hello");
//  _exit(0);
//  printf("hello world\n");
//  exit(10);
//}
